/**
 * Copyright (C), 2022-2025, www.bosssof.com.cn
 * @FileName UserController.java
 * @Author Administrator
 * @Date 2022-9-28  21:35
 * @Description 该文件为具体的用户管理界面的对应的控制器类
 * History:
 * <author> Administrator
 * <time> 2022-9-28  21:35
 * <version> 1.0.0
 * <desc> 该文件为具体的用户管理界面的对应的控制器类
 */
package com.sd365.permission.centre.api.controller;

import com.sd365.permission.centre.api.UserApi;
import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.pojo.dto.UserDTO;
import com.sd365.permission.centre.entity.Department;
import com.sd365.permission.centre.entity.Position;
import com.sd365.permission.centre.entity.Role;
import com.sd365.permission.centre.entity.User;
import com.sd365.permission.centre.pojo.vo.*;
import com.sd365.permission.centre.service.LoginService;
import com.sd365.permission.centre.service.UserService;
import com.sd365.permission.centre.pojo.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author yangshaoqi
 * @Date 2020/12/14 21:11
 * @Version 1.0
 **/
@RestController
public class UserController extends AbstractController implements UserApi {

    /**
     *  用户服务
     */
    @Autowired
    private UserService userService;
    /**
     *  登录服务
     */
    @Autowired
    LoginService loginService;
    //TODO 该方法需要重构可以不可以包含逻辑
    @Override
    public List<ResourceVO> getUserResourceVO(Long id) {
        List<Role> roles = loginService.getRolesByUserId(id);
        List<String> rolesName = new LinkedList<>();
        for (Role role : roles) {
            rolesName.add(role.getName());
        }
        List<ResourceVO> resourceVO = loginService.getResourceVO(roles);
        return resourceVO;
    }

    @ApiLog
    @Override
    public Boolean add(@RequestBody  @Valid UserDTO userDTO) {
        return userService.add(userDTO);
    }

    @ApiLog
    @Override
    public Boolean remove(Long id, Long version) {
        return userService.remove(id, version);
    }

    @ApiLog
    @Override
    public Boolean modify(@Valid UserDTO userDTO) {
        return userService.modify(userDTO);
    }

    @Override
    public Boolean modifyWithNewRole(@Valid UserDTO[] userDTOS) {
        return userService.modifyWithNewRole(userDTOS);
    }

    @ApiLog
    @Override
    public List<User> commonQuery(UserQuery userQuery) {
        List<User> user = userService.commonQuery(userQuery);
        return user;
    }


    @Override
    public UserVO queryUserById(Long id) {
        User user = userService.queryById(id);
        if (user == null) {
            return new UserVO();
        }
        if (user != null) {
            UserVO userVO = BeanUtil.copy(user, UserVO.class);
            if(user.getCompany()!=null){
                userVO.setCompany(BeanUtil.copy(user.getCompany(), CompanyVO.class));
            }
            if(user.getDepartment()!=null){
                userVO.setDepartmentVO(BeanUtil.copy(user.getDepartment(), DepartmentVO.class));
            }
            if(user.getOrganization()!=null){
                userVO.setOrganization(BeanUtil.copy(user.getOrganization(), OrganizationVO.class));
            }
            if(user.getPosition()!=null){
                userVO.setPositionVO(BeanUtil.copy(user.getPosition(),PositionVO.class));
            }
            return userVO;
        } else {
            return null;
        }
    }

    @Override
    @ApiLog
    public List<Role> queryAllRole() {
        return userService.queryAllRole();
    }

    @Override
    @ApiLog
    public List<Department> queryAllDepartment() {
        return userService.queryAllDepartment();
    }

    @Override
    @ApiLog
    public List<Position> queryAllPosition() {
        return userService.queryAllPosition();
    }

    @Override
    public Boolean batchUpdate(@Valid UserDTO[] userDTOS) {
        return userService.batchUpdate(userDTOS);
    }

    @Override
    public Boolean batchDelete(@Valid UserDTO[] userDTOS) {
        return userService.batchDelete(userDTOS);
    }

    @Override
    public Boolean firstStartMd5() {
        return true;
    }

    @Override
    public Boolean updatePassword(@Valid UserDTO userDTO) {
        return userService.updatePassword(userDTO);
    }

    @Override
    public User getUser(String code) {
        return userService.getUser(code);
    }
}
